package com.health.ts.service.sysmanage;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.dao.sysmanage.SysDepartmentMapper;
import com.health.ts.dao.sysmanage.SysUserMapper;
import com.health.ts.entity.sysmanage.SysDepartment;
import com.health.ts.entity.sysmanage.SysUser;
import com.health.ts.service.BaseServiceImpl;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.SessionUserUtil;

/**
 * 科室信息服务
 */
@Service
public class SysDepartmentService extends BaseServiceImpl<SysDepartmentMapper, SysDepartment> {
	
	@Autowired
	SysUserMapper sysUserMapper;

	/**
	 * 查询科室分页列表
	 * @param frontPage
	 * @return
	 */
	public CustomPage<SysDepartment> selectDepartmentPage(FrontPage frontPage) {
		QueryWrapper<SysDepartment> QueryWrapper = new QueryWrapper<SysDepartment>();
		String hospital_id =  getParam(frontPage, "hospital_id");
		if (StringUtils.isNotEmpty(hospital_id)){
			QueryWrapper.eq("hospital_id", hospital_id); 
		}
		
		//医院管理员只返回自己医院科室列表
		if(SessionUserUtil.userIsHosAdmin()){
			QueryWrapper.eq("hospital_id", SessionUserUtil.getUserHosId());
		}
		
		String department_name = getParam(frontPage,"department_name");
		if (StringUtils.isNotEmpty(department_name)){
			QueryWrapper.like("department_name", department_name); 
		}
		QueryWrapper.orderByDesc("update_time");
		Page<SysDepartment> pageList = this.page(frontPage.getPagePlus(), QueryWrapper);
		CustomPage<SysDepartment> customPage = new CustomPage<SysDepartment>(pageList);
		return customPage;
	}
	
	/**
	 * 查询科室列表(不带分页)
	 * @return
	 */
	public List<SysDepartment> listDepartment(String hospitalId) {
		QueryWrapper<SysDepartment> QueryWrapper = new QueryWrapper<SysDepartment>();
		if(StringUtils.isNotEmpty(hospitalId)){
			QueryWrapper.eq("hospital_id", hospitalId);
		}
		//医院管理员只返回自己医院科室列表
		if(SessionUserUtil.userIsHosAdmin()){
			QueryWrapper.eq("hospital_id", SessionUserUtil.getUserHosId());
		}
		
		QueryWrapper.orderByDesc("update_time");
		List<SysDepartment> list=this.list(QueryWrapper);
		return list;
	}
	
	/**
	 * 获取单个科室
	 * @param deptId
	 * @return
	 */
	public SysDepartment selectDepartment(String deptId) {
		SysDepartment sysDepartment=this.getById(deptId);
		AssertUtil.notNull(sysDepartment, "根据入参科室id没有查询到科室信息");
		return sysDepartment;
	}
	
	/**
	 * 科室信息新增
	 * @param department
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addDepartment(SysDepartment department) {
		QueryWrapper<SysDepartment> QueryWrapper = new QueryWrapper<SysDepartment>();
		QueryWrapper.eq("department_code", department.getDepartment_code());
		List<SysDepartment> checkList = this.list(QueryWrapper);
		AssertUtil.empty(checkList, "新增失败：科室编码【"+department.getDepartment_code()+"】已存在");
		Date now=new Date();
		department.setCreate_time(now);
		department.setUpdate_time(now);
		this.save(department);
	}
	
	/**
	 * 科室信息修改
	 * 
	 * @param deptId
	 * @param department
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateDepartment(String deptId, SysDepartment department) {
		SysDepartment info = this.getById(deptId);
		AssertUtil.notNull(info, "要修改的科室不存在！");
		department.setId(deptId);
		department.setUpdate_time(new Date());
		this.updateById(department);
		
		//更新用户表科室名称
		SysUser user = new SysUser();
		user.setDepartment_name(department.getDepartment_name());;
		QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<SysUser>();
		userQueryWrapper.eq("department_id", deptId);
		sysUserMapper.update(user, userQueryWrapper);
	}
	
	/**
	 * 科室信息删除
	 * @param id
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void deleteDepartment(String deptId) {
		//查看科室下是否存在用户
		QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<SysUser>();
		userQueryWrapper.eq("department_id", deptId);
		List<SysUser> checkList = sysUserMapper.selectList(userQueryWrapper);
		AssertUtil.empty(checkList, "该科室下有下属用户，不可删除");
		this.removeById(deptId);
	}
}
